// 修改Blog

function responseJSON(json) {
	if (json) {
		response.write(JSON.stringify(json));
	}
	end();
}

on('post', function () {
	var blog = require('./#BlogLogic.js');

	if (typeof blog == 'string') {
		responseJSON({error: blog});
		return;
	}
	
	var id = request.GET.id ? request.GET.id : request.POST.id;
	var title = request.GET.title ? request.GET.title : request.POST.title;
	var body = request.GET.body ? request.GET.body : request.POST.body; 
	
	if (!id || !title || !body) {
		responseJSON({error: 'Please enter the id, title and body.'});
		return;
	}
	
	blog.connect(function (err) {
		if (err) {
			responseJSON({error: err});
			return;
		}
		else {
			blog.edit({id: id, title: title, body: body}, function (err, resoult) {
				 if (err) {
					responseJSON({error: err});
				 }
				 else {
					responseJSON(resoult);
				 }
			});
		}
	});
	
});